
import Constants from "../libs/Constants";
import GameData from "../libs/GameData";
import Helper from "../libs/HelperMgr";

const { ccclass, property } = cc._decorator;

@ccclass
export default class UISign extends cc.Component {

    @property(cc.Node)
    signs: cc.Node = null;




    start() {
        this.refreshSignList();
    }



    refreshSignList() {
        this.node.getChildByName("Sign").active = !GameData.instance.isSignedToday
        //签到了几天
        let signedDays = GameData.instance.signDays.length;
        for (let i = 1; i <= this.signs.childrenCount; i++) {
            let cell = this.signs.getChildByName("sign" + i);
            let signed = cell.getChildByName('signed');
            signed.active = signedDays >= i;
            let num = cell.getChildByName("num").getComponent(cc.Label);
            num.string = "X" + Constants.SignReward[i-1];
            let select = cell.getChildByName("select");
            if (signedDays > 0) {
                if (GameData.instance.isSignedToday) {
                    select.active = i == signedDays
                } else {
                    select.active = i == signedDays + 1;
                }
            } else {
                select.active = i == 1
            }
        }

    }

    onSign() {
        this.signSuccess()
    }

    signSuccess() {
        let day = (new Date()).getDate();
        GameData.instance.isSignedToday = true;
        GameData.instance.signDays.push(day);
        this.refreshSignList();
        Helper.showUI(Constants.UIName.UIReward);
        this.onClose();
    }




    onClose() {
        GameData.instance.saveData();
        this.node.removeFromParent();
        this.node.destroy();
    }
}
